import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/dark/css-vars.css'

import App from './App.vue'
import router from './router'
import i18n from './i18n'
import '@/styles/index.scss'
import { useThemeStore } from '@/stores/theme'
import { useEncryptionStore } from '@/stores/encryption'

// 只在生产环境注册PWA
if (import.meta.env.PROD) {
  import('./pwa')
}

const app = createApp(App)

// 注册Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

app.use(createPinia())
app.use(router)
app.use(ElementPlus, {
  locale: zhCn,
})
app.use(i18n)

// 初始化主题
const themeStore = useThemeStore()
themeStore.initTheme()

// 初始化加密服务
const encryptionStore = useEncryptionStore()
encryptionStore.initialize().catch(error => {
  console.warn('⚠️ 加密服务初始化失败，将使用明文模式:', error)
})

app.mount('#app')
